package com.xl.rentkeeper.act;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import com.xl.fuse.protos.account.User;
import com.xl.rentkeeper.App;
import com.xl.rentkeeper.R;
import com.xl.rentkeeper.business.UserLogic;
import com.xl.rentkeeper.util.DialogUtil;
import com.xl.rentkeeper.view.TitleBar;

/**
 * Created by Administrator on 2015/7/20.
 */
public class RegisterNo3Act extends BaseAct implements View.OnClickListener {
    private TextView mTVNext;
    private EditText mETPwd,mETRepwd;
    private Dialog mDialog;
    private User mUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_register_no3);
        mUser = (User)getIntent().getSerializableExtra("user");
        //initToolBar();
        TitleBar mTitleBar = (TitleBar) findViewById(R.id.regiNo3_tl);
        initTitleBar(mTitleBar);
        initView();
    }
    private void initView(){
        mETPwd = (EditText) findViewById(R.id.et_pwd);
        mETRepwd = (EditText) findViewById(R.id.et_repwd);
        mTVNext = (TextView) findViewById(R.id.tv_next);
        mTVNext.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (v == mTVNext){
            settingPwd();
        }
    }
    private void settingPwd(){
        String pwd = mETPwd.getText().toString().trim();
        String repwd = mETRepwd.getText().toString().trim();
        if(pwd.length()<6 || pwd.length()>16){
            App.showToast(R.string.pwdformaterr);
            return;
        }
        if(!pwd.equals(repwd)){
            App.showToast(R.string.confirmpwderr);
            return;
        }
        mDialog = DialogUtil.showProgressDialog(this, R.string.loading);
        hideSoftInput(mETRepwd);
        User user =  new User.Builder().phone(mUser.phone).pass(pwd).build();
        UserLogic.getInstance().settingPassword(user);
    }
    public String[] monitorEvents() {
        return new String[]{CMD_USER_SET_PASS};
    }

    @Override
    public void onEvent(String event, boolean ret,String errInfo, Object[] data) {
        super.onEvent(event, ret,errInfo, data);
        if (CMD_USER_SET_PASS.equals(event)) {
            if (mDialog != null && mDialog.isShowing()) mDialog.dismiss();
//            if (ret) {
//                if(data!=null && data.length>0 && data[0]!=null){
//                    mUser = (User) data[0];
//                }
                Intent intent = new Intent(this,RegisterNo4Act.class);
                intent.putExtra("user",mUser);
                startActivity(intent);
                finish();
//            }
        }
    }
    @Override
    public boolean onBack(int keyCode, KeyEvent event) {
        DialogUtil.showBackDialog(this, R.string.tips, R.string.tips_back, R.string.sure, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        },R.string.cancel,null).show();
        return false;
    }
}
